<?php
namespace app\admin\model;

use think\Model;
use expand\Yunst;

class ProjectUser extends Model
{


    /**
     * 后台注册项目方
     * @param array $data 注册信息
     * @author hey_heyang
     * @return 返回结果，status结果(1:成功; 0:失败),msg存放错误信息
     */
    public function registerProjectMember($data=array()){
        $YunClass = new Yunst();
        $result['status'] = 1;
        $result['msg'] = '注册成功';
        $has_mobile = $this->where(array('mobile'=>$data['mobile']))->value('id');
        if($has_mobile>0){
            $result['status'] = 0;
            $result['msg'] = '手机号已注册';
            return $result;
        }
        //首先云商通注册普通用户
        $bizUserId = 'hryqy'.$data['mobile'];
        $res = $YunClass->createQyMember($bizUserId);
        if($res['status']=='OK'){  //云商通注册成功，进行系统注册
            $register_res = $this->register($data);
            if(!$register_res){
                $result['status'] = 0;
                $result['msg'] = '系统注册失败';
            }
        }else{
            $result['status'] = 0;
            $result['msg'] = $res['message'];
        }
        return $result;


    }
    /**
     * 项目方系统数据注册
     * @param array $data 注册信息
     * @author hey_heyang
     * @return true|false
     */
     private function register($data){
         $time_now=time();
         $init_password = 'hry123';
         $salt = generateRandomString(4);
         $data['password'] = sha1(md5($init_password . $salt));
         $data['salt'] = $salt;
         $data['token'] = sha1(md5($data['mobile'] . strval($time_now) . $salt));
         $data['create_time'] = $time_now;
         $data['bizUserId'] = 'hryqy'.$data['mobile'];
//         $data['biz_type'] = 2; //注册时为企业会员
         $insert = $this->insert($data);
         if($insert){
             return true;
         }else{
             return false;
         }
     }


    /**
     *项目方信息审核操作
     * @param $id 项目方ID
     * @param $systerm_check 审核状态
     * @param $systerm_msg 审核意见
     * @author hey_heyang(635870453@qq.com)
     * @return 返回结果，status结果(1:成功; 0:失败),msg存放错误信息
     */
    public function  confirmProjectInfo($id,$systerm_check,$systerm_msg){
        $YunClass = new Yunst();
        $result['status'] = 1;
        $result['msg'] = '操作成功';
        $project_info = $this->where(array('id'=>$id))->find()->toArray();
        if($project_info){
            if($systerm_check==1){  //风控审核通过,接下来向通联发起请求设置企业信息
               $ftp_res = $YunClass->ftpTonglianProjectInfo($project_info['bizUserId'],$project_info);  //首先ftp向通联传送文件
                if($ftp_res){
                    $set_project_res = $YunClass->setProjectCompany($project_info['bizUserId'],$project_info);  //设置企业信息
                    if($set_project_res['status']=='error'){ // 设置企业信息调用失败
                        $result['status']=0;
                        $result['msg'] =$set_project_res['message'];
                    }else{
                        $this->where(array('id'=>$id))->update(array('status'=>2,'systerm_msg'=>$systerm_msg,'systerm_check_time'=>time()));
                    }
                }else{ // ftp上传失败
                    $result['status']=0;
                    $result['msg'] = 'ftp传送失败，请稍后重试!';
                }
            }elseif($systerm_check==2){ // 风控审核不通过
             $this->where(array('id'=>$id))->update(array('status'=>3,'systerm_msg'=>$systerm_msg,'systerm_check_time'=>time()));
                companyTobeNotice($project_info['id'], '项目方信息审核失败', $systerm_msg);
             $result['msg'] = '平台已拒绝';
            }

        }else{
            $result['status']=0;
            $result['msg'] = '未查询到项目方信息';
        }
        return $result;
    }








}